<template>
  <div>
    <el-container>
      <el-header>
        <!--        顶部导航栏开始-->
        <div style="background: chocolate">
          <img src="../assets/logo1.png"
               style="display: inline-block;line-height: 60px;position: absolute;padding-top: 5px;padding-left: 10px;">
          <span style="line-height: 60px;color: aliceblue;margin-left: 100px">
            路漫漫其道修远，吾将上下而求索</span>
          <div style="float: right">
            <el-menu router
                     background-color="chocolate"
                     text-color="aliceblue"
                     active-text-color="#ffd04b"
                     :default-active="activeIndex"
                     class="el-menu-demo" mode="horizontal"
                     @select="handleSelect">
              <el-menu-item v-if="this.isLogin">
                欢迎 {{this.user.nick}}
              </el-menu-item>
              <el-menu-item index="/">首页</el-menu-item>
              <el-submenu v-if="!this.isLogin" index="1">
                <template slot="title">登录/注册</template>
                <el-menu-item index="/login">登录</el-menu-item>
                <el-menu-item index="/register">注册</el-menu-item>
              </el-submenu>
              <el-menu-item index="/shop">购物车</el-menu-item>
              <el-menu-item index="/mine/order">订单</el-menu-item>
              <el-menu-item index="/mine">个人中心</el-menu-item>
              <el-submenu v-if="this.isLogin" index="0">
                <template slot="title">
                  <img :src="this.url + '/'+this.user.avatar" alt="头像"
                       style="display: inline-block;line-height: 60px;width: 50px;height:50px" >
                </template>
                <el-menu-item index="/mine">账号管理</el-menu-item>
                <el-menu-item index="/">登出</el-menu-item>
              </el-submenu>
            </el-menu>
          </div>
        </div>
        <!--        顶部导航栏结束-->
        <div style="margin-top: 30px">
          <!--搜索区域开始-->
          <el-row>
            <el-col :span="8">
              <div class="grid-content bg-purple">
                <span style="font-size: 40px;color: chocolate;font-weight: bold;">万卷书城</span>
              </div>
            </el-col>
            <div style="margin-top: 7px">
              <el-col :span="14">
                <div class="grid-content bg-purple-dark">
                  <el-input v-model="input" placeholder="商品、作者、出版社、ISBN">
                  </el-input>
                </div>
              </el-col>

              <el-col :span="2">
                <div >
                  <el-button onclick="searchBook()" style="margin-left: 7px;background-color: chocolate;color: white">搜索</el-button>
                </div>
              </el-col>
            </div>


          </el-row>
          <el-divider></el-divider>
          <!--搜索区域结束-->
        </div>
      </el-header>
      <el-main>
        <router-view/>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import axios from "axios";
import {mapMutations, mapState} from "vuex";

export default {
  computed: {
    ...mapState(["url", "userId", "user", "isLogin"])
  },
  data() {
    return {
      activeIndex: '1',
      activeIndex2: '1',
      input: '',
    };
  },
  methods: {
    ...mapMutations(["setIsLogin"]),
    logout() {
      axios.get("/user/logout/" + this.userId).catch(e => console.log(e))
      localStorage.removeItem("uid")
      localStorage.removeItem("Authentication")
      this.setIsLogin(false)
      console.log("登出，清楚本地 uid 和 Authentication")
    },
    handleSelect(key, keyPath) {
      console.log(key, keyPath);
      if (key === '/' && keyPath[0] === '0')
        this.logout()
    },
  },
}
</script>

<style>
.el-submenu__title {
  background-color: chocolate;
}
</style>